package org.szx.dao;


import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.type.StringType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.szx.model.ExamRole;
import org.szx.model.ExamSubject;
import org.szx.model.ExamUser;
import org.szx.vo.ExamRoleVO;
import org.szx.vo.ExamUserVO;

@Repository
public class Exam_UserDaoImpl extends BaseHibernateDAO<ExamUser> implements Exam_UserDao {
	
	@Override
	public List<ExamUser> findAllUsers(){
		List<ExamUser> userlist=this.loadAll();
		return userlist;
	}

	@Override
	public void createUser(ExamUser user) throws Exception {
	
		user.setPassword("123456");
		user.setLoginFailCount((byte) 0);
		Iterator<ExamRole> it=user.getExamRoles().iterator();
		
		String sql=null;
		sql="insert into exam_user_role value(:userId,:roleId)";
		while(it.hasNext()){
			this.createSqlQuery(sql).setString("userId", user.getUserId()).setInteger("roleId",it.next().getRoleId()).executeUpdate();
		}
		
		this.save(user);
	}

	@Override
	public ExamUser getUserById(String id){
		return this.get(id);
	}

	@Override
	public void deleteUser(String id) throws Exception {
		String sql=null;
		sql="delete from exam_user_role where user_id=:id";
		this.createSqlQuery(sql).setString("id", id).executeUpdate();
		this.deleteByKey(id);
	}

	@Override
	public void editUser(ExamUser user) throws Exception {
		user.setPassword(this.get(user.getUserId()).getPassword());
		this.merge(user);
	}

	@Override
	public ExamUserVO getUserById1(String id) {
		// TODO Auto-generated method stub
		ExamUser entity=this.get(id);
		if (entity != null) {
			ExamUserVO euVO = new ExamUserVO();
			BeanUtils.copyProperties(entity, euVO);

			ExamRoleVO erVO = new ExamRoleVO();
			Set<ExamRoleVO> ExamRoleVO = new HashSet<ExamRoleVO>();
			for (ExamRole examRole : entity.getExamRoles()) {
				BeanUtils.copyProperties(examRole,
						erVO);
				ExamRoleVO.add(erVO);
			}
			euVO.setExamRoleVO(ExamRoleVO);
			euVO.setRoleId(erVO.getRoleId());

			return euVO;

		}
		return null;
	}

	@Override
	public void updateUserLoginFailCount(ExamUserVO resultVO) {
		// TODO Auto-generated method stub
		ExamUser examUser=this.get(resultVO.getUserId());
		examUser.setLoginFailCount(resultVO.getLoginFailCount());
		this.merge(examUser);
	}

	@Override
	public void updateUserLastLockTime(ExamUserVO resultVO) {
		// TODO Auto-generated method stub
		ExamUser examUser=this.get(resultVO.getUserId());
		examUser.setLastLockTime(resultVO.getLastLockTime());
		this.merge(examUser);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ExamSubject> getAllSubjectByUser(String userId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select subject.sub_Id as subId ");
		sql.append(" from exam_user_subject eus ");
		sql.append(" left join exam_user es on eus.user_id = es.user_id ");
		sql.append(" left join exam_subject subject on subject.sub_id = eus.sub_id ");
		sql.append(" where es.user_id = :userId ");
		return this.createSqlQuery(String.valueOf(sql))
				.addScalar("subId",StringType.INSTANCE)
				.setString("userId", userId)
				.list();
	}


}
